<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/4/8
 * Time: 9:38
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Banner extends Base
{
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('banner')->count();// 获取总条数
            $lists = Db::name('banner')
                ->field('banner_id, banner_title, banner_img, banner_status, banner_cate, banner_type, 
                banner_link, banner_sort, banner_time')
                ->page($Nowpage,$limits)
                ->order('banner_sort desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            $banner_title = trim(input('banner_title'), ' ');
            $banner_img = input('banner_img');
            $banner_status = input('banner_status');
            $banner_sort = input('banner_sort');
            $banner_cate = input('banner_cate');
            $banner_type = input('banner_type');
            $assn_id = input('assn_id');
            $information_id = input('information_id');
            $post_id = input('post_id');
            $video_id = input('video_id');
            $answer_id = input('answer_id');
            $banner_link = input('banner_link');
            if($banner_cate == 2 && $banner_link == ''){
                return json(['code' => 100, 'msg' => '请填写外链地址']);
            }
            $data = [
                'banner_title' => $banner_title,
                'banner_img' => $banner_img,
                'banner_cate' => $banner_cate,
                'banner_sort' => $banner_sort,
                'banner_status' => $banner_status,
                'banner_time' => date('Y-m-d H:i:s')
            ];
            if($banner_cate == 1){ // 内链
                $data['banner_type'] = $banner_type;
                if($banner_type == 1){ // 社团
                    $data['banner_type_id'] = $assn_id;
                }elseif($banner_type == 2){ // 资讯
                    $data['banner_type_id'] = $information_id;
                }elseif($banner_type == 3){ // 军婚军恋
                    $data['banner_type_id'] = $post_id;
                }elseif($banner_type == 4){ // 短视频
                    $data['banner_type_id'] = $video_id;
                }else{ // 问答
                    $data['banner_type_id'] = $answer_id;
                }
            }else{ // 外链
                $data['banner_link'] = $banner_link;
            }
            $res = Db::table('up_banner')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        // 社团
        $assn = Db::table('up_assn')
            ->field('assn_id, assn_name')
            ->where('assn_status', 1)
            ->order('assn_time desc')
            ->select();
        // 资讯
        $information = Db::table('up_information')
            ->field('information_title, information_id')
            ->order('information_fa_time desc')
            ->select();
        // 军婚军恋
        $love = Db::table('up_love_post')
            ->field('post_id, post_content')
            ->where('post_status', 1)
            ->where('post_is_report', 1)
            ->order('post_create_time desc')
            ->select();
        // 短视频
        $video = Db::table('up_video')
            ->field('video_id, video_title')
            ->where('video_status', 2)
            ->order('video_time desc')
            ->select();
        // 悬赏问答
        $answer = Db::table('up_answer')
            ->field('answer_id, answer_title')
            ->order('answer_time desc')
            ->select();
        
        $this->assign([
            'assn' => $assn,
            'informatioin' => $information,
            'love' => $love,
            'video' => $video,
            'answer' => $answer
        ]);
        return $this->fetch('banner/banneradd');
    }
    
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            $id = input('banner_id');
            $banner_title = trim(input('banner_title'), ' ');
            $banner_img = input('banner_img');
            $banner_cate = input('banner_cate');
            $banner_type = input('banner_type');
            $assn_id = input('assn_id');
            $information_id = input('information_id');
            $post_id = input('post_id');
            $video_id = input('video_id');
            $answer_id = input('answer_id');
            $banner_link = input('banner_link');
            if($banner_cate == 2 && $banner_link == ''){
                return json(['code' => 100, 'msg' => '请填写外链地址']);
            }
            $data = [
                'banner_title' => $banner_title,
                'banner_cate' => $banner_cate,
            ];
            
            if(!empty($banner_img)){
                $data['banner_img'] = $banner_img;
            }
            if($banner_cate == 1){ // 内链
                $data['banner_type'] = $banner_type;
                $data['banner_link'] = null;
                if($banner_type == 1){ // 社团
                    $data['banner_type_id'] = $assn_id;
                }elseif($banner_type == 2){ // 资讯
                    $data['banner_type_id'] = $information_id;
                }elseif($banner_type == 3){ // 军婚军恋
                    $data['banner_type_id'] = $post_id;
                }elseif($banner_type == 4){ // 短视频
                    $data['banner_type_id'] = $video_id;
                }else{ // 问答
                    $data['banner_type_id'] = $answer_id;
                }
            }else{ // 外链
                $data['banner_link'] = $banner_link;
                $data['banner_type_id'] = null;
                $data['banner_type'] = null;
            }

            $res = Db::table('up_banner')->where('banner_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner_id = input('banner_id');
        // banner
        $banner = Db::table('up_banner')
            ->where('banner_id', $banner_id)
            ->find();
        // 社团
        $assn = Db::table('up_assn')
            ->field('assn_id, assn_name')
            ->where('assn_status', 1)
            ->order('assn_time desc')
            ->select();
        // 资讯
        $information = Db::table('up_information')
            ->field('information_title, information_id')
            ->order('information_fa_time desc')
            ->select();
        // 军婚军恋
        $love = Db::table('up_love_post')
            ->field('post_id, post_content')
            ->where('post_status', 1)
            ->where('post_is_report', 1)
            ->order('post_create_time desc')
            ->select();
        // 短视频
        $video = Db::table('up_video')
            ->field('video_id, video_title')
            ->where('video_status', 2)
            ->order('video_time desc')
            ->select();
        // 悬赏问答
        $answer = Db::table('up_answer')
            ->field('answer_id, answer_title')
            ->order('answer_time desc')
            ->select();
    
        $this->assign([
            'assn' => $assn,
            'informatioin' => $information,
            'love' => $love,
            'video' => $video,
            'answer' => $answer,
            'banner_id' => $banner_id,
            'banner' => $banner
        ]);
        return $this->fetch('banner/banneredit');
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function bannerOn(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->setField('banner_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 200, 'msg' => '上架失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDown(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->setField('banner_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 200, 'msg' => '下架失败']);
        }
    }
    
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'banner_sort'=>input('value'),
        ];
        $res = Db::name('banner')->where('banner_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function bannerDel(){
        $id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}